var shareDAO = require('../models/shareDAO')
var formidable = require('formidable')
var path = require('path')
var shareController = {
        //分享圈
        shareContent: function (req, res) {
            shareDAO.shareContent(function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '用户分享失败' })
                } else {
                    res.json({ code: 200, data: results, msg: '用户分享成功!' })

                }
            })
        },

        //已发布内容
        publish: function (req, res) {
            var userId = req.params.userId
            shareDAO.publish(userId, function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '获取用户发布内容失败' })
                } else {
                    if (results.length > 0) {
                        res.json({ code: 200, data: results, msg: '用户发布内容成功!' })
                    } else {
                        res.json({ code: 200, msg: '获取用户发布内容失败' })
                    }
                }
            })
        },

        

        //分享圈详情
        sharedetail: function (req, res) {
            var upmenuId = req.params.upmenuId
            shareDAO.sharedetail(upmenuId, function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '用户获取分享圈详情失败' })
                } else {
                    if (results.length > 0) {
                        res.json({ code: 200, data: results, msg: '用户获取分享圈详情成功!' })
                    } else {
                        res.json({ code: 200, msg: '用户获取分享圈详情失败' })
                    }
                }
            })
        },

        //已发布内容的详情
        publishdetail: function (req, res) {
            var upmenuId = req.params.upmenuId
            shareDAO.publishdetail(upmenuId, function (err, results) {
                if (err) {
                    res.json({ code: 500, msg: '用户获取发布内容详情失败' })
                } else {
                    if (results.length > 0) {
                        res.json({ code: 200, data: results, msg: '用户获取发布内容详情成功!' })
                    } else {
                        res.json({ code: 200, msg: '用户获取发布内容详情失败' })
                    }
                }
            })
        },

        //上传菜谱new
        upmenu:function(req,res){
            var upmenuone={
                upmenuTitle:req.body.upmenuTitle,
                finishPic:req.body.finishPic,
                story:req.body.story,
                touserId:req.body.touserId
            }
            shareDAO.upmenu(upmenuone,function(err,results){
                if(err){
                    res.json({code:500,msg:'上传菜谱第一步数据库错误！'})
                }else{
                    if(results.affectedRows!=0){
                        // console.log(results[0].upmenuId)
                        // res.json({code:200,data:results.affectedRows,msg:'上传菜谱第一步成功！'})
                        //搜索当前插入的菜谱第一步的id值
                        shareDAO.lookUpmenu(upmenuone,function(err,lookresults){
                            if(err){
                                res.json({code:500,msg:'上传菜谱第一步搜索数据库错误！'})
                            }else{
                                if(lookresults.affectedRows!=0){
                                    console.log(lookresults[0].upmenuId)
                                    // res.json({code:200,data:lookresults,msg:'上传菜谱第一步搜索成功！'})
                                    var toupmenuId=lookresults[0].upmenuId
                                    var stepmake={
                                        step:req.body.step

                                    }
                                    console.log(stepmake)
                                    //插入步骤
                                    shareDAO.insertStep(stepmake,toupmenuId,function(err,stepresults){
                                        if(err){
                                            res.json({code:500,msg:'插入步骤数据库错误！'})
                                        }else{
                                            if(stepresults.affectedRows!=0){
                                                res.json({code:200,data:stepresults.affectedRows,msg:'插入步骤成功！'})
                                            }else{
                                                // res.json({code:200,data:stepresults.affectedRows,msg:'插入步骤失败！'})
                                                //如果插入不成功，则步骤一的数据库删除记录
                                                shareDAO.delupmenu(toupmenuId,function(err,delresults){
                                                    if(err){
                                                        res.json({code:500,msg:'删除上传菜谱第一步数据库错误！'})
                                                    }else{
                                                        if(delresults.affectedRows!=0){
                                                            res.json({code:200,data:delresults.affectedRows,msg:'删除上传菜谱第一步成功！'})
                                                        }else{
                                                            res.json({code:200,data:delresults.affectedRows,msg:'删除上传菜谱第一步失败！'})
                                                        }
                                                    }
                                                })
                                            }
                                        }
                                    })
                                }else{
                                    res.json({code:200,data:lookresults.affectedRows,msg:'上传菜谱第一步搜索失败！'})
                                }
                            }
                        })
                    }else{
                        res.json({code:200,data:results.affectedRows,msg:'上传菜谱第一步失败！'})
                    }
                }
            })
        },




        //删除已发布菜谱
        personal: function (req, res) {
            var upmenuId = req.params.upmenuId
            shareDAO.personalConment(upmenuId, function (err, results) {
                if (err) { 
                    res.json({ code: 500, msg: '删除已上传的菜谱数据库错误' })
                } else {
                    if(results.affectedRows!=0){
                        shareDAO.deletepersonal(upmenuId, function (err, delresults){
                            if(err){
                                res.json({code:500, msg:'删除失败'})
                            }else{
                                if(delresults.affectedRows!=0){
                                    res.json({ code:200, data:results,msg:'删除成功！'})
                                }else{
                                    res.json({ code:200, data:results,msg:'删除失败！'})
                                }
                                
                            }
                         })
                    }else{
                        res.json({code:200,data:results.affectedRows,msg:'删除已上传的菜谱失败！'})
                    }
                   
                }
            })
        },   

 //增加分享圈点赞数
 addUserlike: function (req, res) {
    var userId = req.body.userId
    shareDAO.selsharelike(userId, function (err, results) {
        if (err) {
            res.json({ code: 500, msg: '查询点赞数失败' })
        } else {
            if (results.length >= 0) {
                var upmenuId = req.body.upmenuId
                var fs = 0;
                var flag = 1;
                if (results.length != 0) {
                    for (var i = 0; i < results.length; i++) {
                        if (upmenuId == results[i].toupmenuId) {
                            res.json({ code: 200, data: 0, msg: '该用户已经点过赞' })
                            flag = 0
                        }
                    }
                } else if (results.length == 0) {
                    fs = 1;
                    console.log(fs)
                }
                if (flag == 1 || fs == 1) {
                    console.log(userId)
                    shareDAO.insertLike({ upmenuId: upmenuId, userId: userId }, function (err, resinsert) {
                        if (err) {
                            res.json({ code: 500, msg: '在数据库中插入一条点赞记录失败！' })
                        } else {
                            if (resinsert.affectedRows == 1) {
                                shareDAO.addUserlike(upmenuId, function (err, resadd) {
                                    if (err) {
                                        res.json({ code: 500, msg: '数据库点赞增加错误' })
                                    } else {
                                        if (resadd.affectedRows == 1) {
                                            res.json({ code: 200, data: resadd.affectedRows, msg: '点赞成功' })
                                        } else {
                                            res.json({ code: 200, data: resadd.affectedRows, msg: '点赞失败' })
                                        }
                                    }
                                })
                            } else {
                                res.json({ code: 200, data: resinsert.affectedRows, msg: '插入一条点赞记录失败' })
                            }
                        }
                    })
                }
            } else {
                res.json({ code: 200, data: results, msg: '该用户没有点赞记录' })
            }
        }
    })
},
//取消分析圈点赞数
celUserlike: function (req, res) {
    var userId = req.body.userId
    shareDAO.selsharelike(userId, function (err, results) {
        if (err) {
            res.json({ code: 500, msg: '查询数据库失败' })
        } else {
            if (results.length != 0){
            var upmenuId = req.body.upmenuId
            var flag = 1
            for(var i = 0;i<results.length;i++){
                if(upmenuId == results[i].toupmenuId){
                    shareDAO.delLike({upmenuId:upmenuId,userId:userId},function(err,resdelete){
                        if (err) {
                              console.log(userId)
                              console.log(upmenuId)
                            res.json({ code: 500, msg: '在数据库中删除点赞记录失败！' })
                    }else{
                        if(resdelete.affectedRows == 1){
                          
                            shareDAO.celUserlike(upmenuId,function(err,resdel){
                                if(err){
                                    res.json({code:500,msg:'数据库点赞取消失败'})
                                }else{
                                    if (resdel.affectedRows == 1) {
                                        res.json({ code: 200, data: resdel.affectedRows, msg: '取消点赞成功！' })
                                    } else {
                                        res.json({ code: 200, data: resdel.affectedRows, msg: '取消点赞失败！' })
                                    }
                                }
                            })
                        }else {
                            res.json({ code: 200, data: resdelete.affectedRows, msg: '删除点赞记录失败！' })
                    }
                    
                }
            })
            flag=0
            }
        }
        if(flag==1){
            res.json({ code: 200, data: 0, msg: '该用没有点赞过这个套餐！' });
        }
    }else{
            res.json({ code: 200, data: results, msg: '取消点赞数成功' })
        }
    }

    })
}
    }
module.exports = shareController                                                                                                                                                                                                                                                   